if lunocartas ==nil then
	lunocartas = {}
end

lunocartas.mailbox = { }


lunocartas.openinbox = function(player)
	if player ~= nil and player:is_player() then
		local playername = player:get_player_name()
		if lunocartas.mailbox[playername]==nil or 
			lunocartas.mailbox[playername].selmail == nil or 
			type(lunocartas.mailbox[playername].selmail)~="number" or 
			lunocartas.mailbox[playername].selmail<0 
		then
			lunocartas.mailbox[playername] = { }
			lunocartas.mailbox[playername].selmail = 0
		end
		
		local formspecmails = lunocartas.get_formspecmails(playername)
		--minetest.log('action',"formspecmails = "..formspecmails)
		local formspec = "size[8,7.5]"
		.."label[2.75,0;CORREIO]"
		.."button_exit[0.5,7;1.5,0.5;closer;FECHAR]"
		.."button[2.0,7;1.5,0.5;openmail;ABRIR]"
		.."button[3.5,7;1.5,0.5;delmail;EXCLUIR]"
		.."button[5.0,7;1.5,0.5;clearmails;LIMPAR]"
		.."textlist[0,0.7;7.7,6;selmail;"..formspecmails..";"..lunocartas.mailbox[playername].selmail..";false]"
		
		minetest.show_formspec(player:get_player_name(),"lunocartas.mailbox",formspec)
	end
end

lunocartas.openmail = function(player, mailnumber)
	if player ~= nil and player:is_player() then
		local playername = player:get_player_name()
		local mail = lunocartas.get_mail(playername, mailnumber)
		if mail~=nil then
			--local formspecmails = lunocartas.get_formspecmails(playername)
			--minetest.log('action',"formspecmails = "..formspecmails)
			minetest.log('action',"[LUNOCARTAS] mail = "..dump(mail))
			local formspec = "size[8,7.5]"
			--.."label[2.75,0;MENSAGEM DE: "..mail.namefrom.."]"
			.."label[0.2,0;From: "..mail.namefrom.."]"
			.."button_exit[0.5,7;2,0.5;closer;FECHAR]"
			.."button[2.5,7;2,0.5;openinbox;VOLTAR]"
			.."button[4.5,7;2,0.5;delmail;EXCLUIR]"
			.."textarea[0.5,1.0;7.7,6.5;message;"..
			minetest.formspec_escape(os.date("%Y-%m-%d %Hh:%Mm:%Ss", mail.time))..";"..minetest.formspec_escape(mail.message).."]"
			--.."textlist[0,0.7;15.5,6;selmail;"..formspecmails..";1;false]"
		
			lunocartas.set_read(playername, mailnumber, true)
			lunocartas.save()
		
			minetest.show_formspec(player:get_player_name(),"lunocartas.mailbox",formspec)
		end
	end
end

minetest.register_on_player_receive_fields(function(sender, formname, fields)
	if formname == "lunocartas.mailbox"  then
		local sendername = sender:get_player_name()
		minetest.log('action',"[LUNOCARTAS] fields = "..dump(fields))
		if fields.openinbox then
			minetest.log('action',"lunocartas.openinbox("..sendername..")")
			lunocartas.openinbox(sender)
		elseif fields.selmail then
			local selnum = (fields.selmail):gsub("CHG:", "")
			minetest.log('action',"lunocartas.mailbox[sendername].selmail="..dump(lunocartas.mailbox[sendername].selmail))
			lunocartas.mailbox[sendername].selmail = tonumber(selnum)
		elseif fields.openmail~=nil then
			if lunocartas.mailbox[sendername].selmail~=nil and type(lunocartas.mailbox[sendername].selmail)=="number" and lunocartas.mailbox[sendername].selmail >=1 then
				minetest.log('action',"lunocartas.openmail["..sendername.."].selmail="..dump(lunocartas.mailbox[sendername].selmail))
				lunocartas.openmail(sender, lunocartas.mailbox[sendername].selmail)
			else
				minetest.chat_send_player(sendername, "Selecione a carta que deseja abrir!")
			end
		elseif fields.delmail~=nil then
			if lunocartas.mailbox[sendername].selmail~=nil and type(lunocartas.mailbox[sendername].selmail)=="number" and lunocartas.mailbox[sendername].selmail >=1 then
				minetest.log('action',"lunocartas.del_mail("..sendername..", "..lunocartas.mailbox[sendername].selmail..")")
				lunocartas.del_mail(sendername, lunocartas.mailbox[sendername].selmail)
				lunocartas.openinbox(sender)
			else
				minetest.chat_send_player(sendername, "Selecione a carta que deseja excluir!")
			end
		elseif fields.clearmails~=nil then
			minetest.log('action',"lunocartas.chat_delreadeds("..sendername..")")
			lunocartas.chat_delreadeds(sendername)
			lunocartas.openinbox(sender)
		end
	end
end)

local mailbox_format = {
	type = "fixed",
	fixed = { 
		{-.25,-.25,.5,			.25,.356,.45}
	}
}
local mailbox_tiles = {
	"tex_mailbox_topdown.png", --cima
	"tex_mailbox_topdown.png", --baixo
	"tex_mailbox_sides.png", --direita
	"tex_mailbox_sides.png".."^[transformFX", --esquerda
	"tex_mailbox_back.png", --traz
	"tex_mailbox_from.png", --frente
	--
}

minetest.register_node("lunocartas:mailbox", {
	description = "Caixa de Correio",
	inventory_image = "obj_mailbox.png",
	--inventory_image = minetest.inventorycube("tex_light.png"),
	drawtype = "nodebox",
	paramtype = "light",
	--paramtype2 = "fixed",
	paramtype2 = "facedir",
	walkable = false,
	node_box =mailbox_format,
	selection_box = mailbox_format,
	tiles = mailbox_tiles,
	--sunlight_propagates = true,
	--light_source = LIGHT_MAX,
	is_ground_content = true, --Nao tenho certeza: Se prega no chao?
	groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2}, --{cracky=3,oddly_breakable_by_hand=3},
	sounds = default.node_sound_wood_defaults(), --default.node_sound_glass_defaults(),
	after_place_node = function(pos, placer, itemstack)
		local meta = minetest.env:get_meta(pos)
		local owner = placer:get_player_name()
		meta:set_string("infotext", "Caixa de Correio de '"..owner.."'")
		meta:set_string("owner",owner)
	end,
	can_dig = function(pos, player)
		local meta = minetest.env:get_meta(pos)
		if meta~=nil then
			local ownername = meta:get_string("owner")
			local playername = player:get_player_name()
			if ownername~=nil and ownername~="" and ownername~=playername then
				minetest.chat_send_player(playername, "Voce nao pode destruir a caixa de correio de "..ownername..".")
				return false
			else
				return true
			end
		else
			return true
		end
	end,
	on_rightclick = function(pos, node, clicker, itemstack)
		local meta = minetest.env:get_meta(pos)
		if meta~=nil then
			local ownername = meta:get_string("owner")
			if ownername~=nil and ownername~="" then
				local ownerplayer = minetest.env:get_player_by_name(ownername)
				local clickername = clicker:get_player_name()
				
				if ownername==clickername then --Verificar se o don oesta online
					lunocartas.openinbox(ownerplayer, meta)
				else
					minetest.chat_send_player(clickername, "Esta caixa de correio nao lhe pertece!")
				end
			end
		end
	end,
})

minetest.register_craft({
	output = 'lunocartas:mailbox',
	recipe = {
		{"group:wood"	,"group:wood"		,"group:wood"},
		{"group:wood"	,"default:paper"	,"group:wood"},
		{"group:wood"	,"group:wood"		,"group:wood"},
	}
})

minetest.register_alias("mailbox"			,"lunocartas:mailbox")
minetest.register_alias("correio"			,"lunocartas:mailbox")
minetest.register_alias("caixacorreio"		,"lunocartas:mailbox")
minetest.register_alias("caixadecorreio"	,"lunocartas:mailbox")
